package com.example.exam.controller;

import com.example.exam.entity.*;
import com.example.exam.service.*;
import com.example.exam.util.RedisUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class loginController {

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RoleService roleService;
    @Resource
    private MajorService majorService;
    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private AdminService adminService;
    @Resource
    private AssistService assistService;

    /**
     * 点击登陆触发事件
     *
     * @param username 用户名
     * @param password 密码
     * @param session
     * @return
     */
    @RequestMapping("loginPost")
    public void loginTea(String username, String password, HttpSession session, HttpServletResponse response) throws IOException {
        int i = 0;
        String temp = username + password;
        Role role = (Role) redisUtils.get(temp);
        if (role == null) {
            role = roleService.queryByIdAndName(username, password);
            redisUtils.set(temp, role);
            redisUtils.expire(temp, 60 * 60 * 60);
        }
        int userId = role.getuserId();
        session.setAttribute("userId", userId);
        String perms = role.getPerms();
        if ("assist".equals(perms)) {
            response.sendRedirect("assist/index.html");
        } else if ("teacher".equals(perms)) {
            response.sendRedirect("teacher/index.html");
        } else if ("student".equals(perms)) {
            response.sendRedirect("student/html/select_page.html");
        } else if ("admin".equals(perms)) {
            response.sendRedirect("admin/index.html");
        }
    }

    @RequestMapping("/reg")
    public String reg() {
        return "reg";
    }

    @RequestMapping("/completeReg")
    public void comReg(String iden, String youname, String tel, String username1, String password1, HttpSession session, HttpServletResponse response) throws IOException {
        int i = 0;
        if ("学生".equals(iden)) {
            Student student = new Student();
            student.setStuname(youname);
            student.setTel(tel);
            Student temp = studentService.insert(student);
            Integer stuId = temp.getStuid();
            session.setAttribute("userId", stuId);
            Role role = new Role();
            role.setuserId(stuId);
            role.setroleName(username1);
            role.setPassword(password1);
            String res = username1 + password1;
            redisUtils.set(res, role);
            redisUtils.expire(res, 60 * 60 * 60);
            Role insert = roleService.insert(role);
            session.setAttribute("role", insert);
            response.sendRedirect("student/html/select_page.html");
        } else if ("老师".equals(iden)) {
            Teacher teacher = new Teacher();
            teacher.setTecname(youname);
            teacher.setTel(tel);
            Teacher temp = teacherService.insert(teacher);
            Integer stuId = temp.getTecid();
            session.setAttribute("userId", stuId);
            Role role = new Role();
            role.setuserId(stuId);
            role.setroleName(username1);
            role.setPassword(password1);
            String res = username1 + password1;
            redisUtils.set(res, role);
            redisUtils.expire(res, 60 * 60 * 60);
            Role insert = roleService.insert(role);
            session.setAttribute("role", insert);
            response.sendRedirect("teacher/index.html");
        } else if ("助教".equals(iden)) {
            Assist assist = new Assist();
            assist.setAssname(youname);
            assist.setTel(tel);
            Assist temp = assistService.insert(assist);
            Integer stuId = temp.getAssid();
            session.setAttribute("userId", stuId);
            Role role = new Role();
            role.setuserId(stuId);
            role.setroleName(username1);
            role.setPassword(password1);
            String res = username1 + password1;
            redisUtils.set(res, role);
            redisUtils.expire(res, 60 * 60 * 60);
            Role insert = roleService.insert(role);
            session.setAttribute("role", insert);
            response.sendRedirect("assist/index.html");
        } else if ("管理员".equals(iden)) {
            Admin admin = new Admin();
            admin.setAdminname(youname);
            admin.setTel(tel);
            Admin temp = adminService.insert(admin);
            Integer stuId = temp.getAdminId();
            session.setAttribute("userId", stuId);
            Role role = new Role();
            role.setuserId(stuId);
            role.setroleName(username1);
            role.setPassword(password1);
            String res = username1 + password1;
            redisUtils.set(res, role);
            redisUtils.expire(res, 60 * 60 * 60);
            Role insert = roleService.insert(role);
            session.setAttribute("role", insert);
            response.sendRedirect("admin/index.html");
        }
    }
}